<!DOCTYPE html>
<html lang="zh-cn">
<head prefix="og: http://ogp.me/ns#">
  <meta charset="utf-8">
  <title>全局对象 | Screeps 中文文档</title>
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!-- Canonical links -->
  <link rel="canonical" href="http://screeps-cn.github.io/global-objects.html">
  <!-- Alternative links -->
  
    
      <link rel="alternative" hreflang="en" href="http://screeps-cn.github.io/global-objects.html">
    
      <link rel="alternative" hreflang="zh-tw" href="http://screeps-cn.github.io/zh-tw/global-objects.html">
    
      <link rel="alternative" hreflang="zh-cn" href="http://screeps-cn.github.io/zh-cn/global-objects.html">
    
      <link rel="alternative" hreflang="ru" href="http://screeps-cn.github.io/ru/global-objects.html">
    
      <link rel="alternative" hreflang="ko" href="http://screeps-cn.github.io/ko/global-objects.html">
    
  
  <!-- Icon -->
  <link rel="apple-touch-icon" sizes="57x57" href="/icon/apple-touch-icon-57x57.png">
  <link rel="apple-touch-icon" sizes="114x114" href="/icon/apple-touch-icon-114x114.png">
  <link rel="apple-touch-icon" sizes="72x72" href="/icon/apple-touch-icon-72x72.png">
  <link rel="apple-touch-icon" sizes="144x144" href="/icon/apple-touch-icon-144x144.png">
  <link rel="apple-touch-icon" sizes="60x60" href="/icon/apple-touch-icon-60x60.png">
  <link rel="apple-touch-icon" sizes="120x120" href="/icon/apple-touch-icon-120x120.png">
  <link rel="apple-touch-icon" sizes="76x76" href="/icon/apple-touch-icon-76x76.png">
  <link rel="apple-touch-icon" sizes="152x152" href="/icon/apple-touch-icon-152x152.png">
  <link rel="icon" type="image/png" href="/icon/favicon-196x196.png" sizes="196x196">
  <link rel="icon" type="image/png" href="/icon/favicon-160x160.png" sizes="160x160">
  <link rel="icon" type="image/png" href="/icon/favicon-96x96.png" sizes="96x96">
  <link rel="icon" type="image/png" href="/icon/favicon-16x16.png" sizes="16x16">
  <link rel="icon" type="image/png" href="/icon/favicon-32x32.png" sizes="32x32">
  <meta name="msapplication-TileColor" content="#2f83cd">
  <meta name="msapplication-TileImage" content="/icon/mstile-144x144.png">
  <!-- CSS -->
  <!-- build:css build/css/navy.css -->
  <link rel="stylesheet" href="/css/navy.css?1">
  <link rel="stylesheet" href="/css/prism.css">
  <!-- endbuild -->
  <link href="https://fonts.googleapis.com/css?family=Lato:300,400,700" rel="stylesheet" type="text/css">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/docsearch.js/1/docsearch.min.css">
  <!-- RSS -->
  <link rel="alternate" href="/atom.xml" title="Screeps 中文文档">
  <!-- Open Graph -->
  <meta name="description" content="Game 对象你可以通过全局对象 Game 来操作游戏，它的详细介绍可以在 API 参考 中找到。该对象可以使您访问到您 creep 的完整列表、“查阅”房间、传递命令等。
var target = Game.spawns.Spawn1;
for(var i in Game.creeps) {
    Game.creeps[i].moveTo(target);
}tick 的流动不会改变 Gam">
<meta property="og:type" content="website">
<meta property="og:title" content="全局对象">
<meta property="og:url" content="http://screeps-cn.github.io/global-objects.html">
<meta property="og:site_name" content="Screeps 中文文档">
<meta property="og:description" content="Game 对象你可以通过全局对象 Game 来操作游戏，它的详细介绍可以在 API 参考 中找到。该对象可以使您访问到您 creep 的完整列表、“查阅”房间、传递命令等。
var target = Game.spawns.Spawn1;
for(var i in Game.creeps) {
    Game.creeps[i].moveTo(target);
}tick 的流动不会改变 Gam">
<meta property="og:updated_time" content="2024-09-20T13:38:58.408Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="全局对象">
<meta name="twitter:description" content="Game 对象你可以通过全局对象 Game 来操作游戏，它的详细介绍可以在 API 参考 中找到。该对象可以使您访问到您 creep 的完整列表、“查阅”房间、传递命令等。
var target = Game.spawns.Spawn1;
for(var i in Game.creeps) {
    Game.creeps[i].moveTo(target);
}tick 的流动不会改变 Gam">
  <!-- Google Analytics -->
  
</head>

<body>
  <div id="container">
    <header id="header" class="wrapper">
  <div id="header-inner" class="inner">
    <h1 id="logo-wrap">
      <a href="https://screeps.com" id="logo">Screeps</a>
      <a href="/index.html" id="logo-docs">docs</a>
    </h1>
    <a id="mobile-nav-toggle">
      <span class="mobile-nav-toggle-bar"></span>
      <span class="mobile-nav-toggle-bar"></span>
      <span class="mobile-nav-toggle-bar"></span>
    </a>
    <div id="header-main"></div>
  </div>
</header>

    <div id="content-wrap">
  <div id="content" class="wrapper">
    <div id="content-inner">
      <article class="article-container" itemscope itemtype="http://schema.org/Article">
        <div class="article-inner">
          <div class="article">
            <div class="inner">
              <header class="article-header">
                <h1 class="article-title" itemprop="name" id="top">全局对象</h1>
                <a href="https://github.com/screeps-cn/docs/edit/master/source/global-objects.md" class="article-edit-link" title="改进本文"><i class="fa fa-pencil"></i></a>
              </header>
              <div class="article-content" itemprop="articleBody">
                
                <h2 id="Game-对象" class="article-heading"><a href="#Game-对象" class="headerlink" title="Game 对象"></a><code>Game</code> 对象<a class="article-anchor" href="#Game-对象" aria-hidden="true"></a></h2><p>你可以通过全局对象 <a href="/api/#Game"><code>Game</code></a> 来操作游戏，它的详细介绍可以在 <a href="/api/">API 参考</a> 中找到。该对象可以使您访问到您 creep 的完整列表、“查阅”房间、传递命令等。</p>
<pre class="highlight javascript tab-javascript "><code><span class="token keyword">var</span> target <span class="token operator">=</span> Game<span class="token punctuation">.</span>spawns<span class="token punctuation">.</span>Spawn1<span class="token punctuation">;</span>
<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token keyword">in</span> Game<span class="token punctuation">.</span>creeps<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    Game<span class="token punctuation">.</span>creeps<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">moveTo</span><span class="token punctuation">(</span>target<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><p>tick 的流动不会改变 <code>Game</code> 的状态。即使您手动修改了该对象的某个属性，它也不会在游戏中生效。想要修改属性和传递命令只能使用特殊的游戏对象方法。</p>
<p><code>Game</code> 对象在游戏开始时就已经被创建，并且每个 tick 时都会被刷新数据。如果想在 tick 之间存储信息，可以使用 Memory 对象。阅读下一篇文章来了解更多信息。</p>
<h2 id="Memory-对象" class="article-heading"><a href="#Memory-对象" class="headerlink" title="Memory 对象"></a><code>Memory</code> 对象<a class="article-anchor" href="#Memory-对象" aria-hidden="true"></a></h2><p>每个玩家都可以访问全局对象 <code>Memory</code>，并且他/她可以在其中保存任何 JSON 格式的数据。所有写入其中的数据都将自动的使用<code>JSON.stringify</code>进行保存并在每个 tick 之间传递。因此，您可以借此来保存设置、决策信息和临时数据。</p>
<pre class="highlight undefined tab-undefined "><code>Memory<span class="token punctuation">.</span>someData <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token operator">...</span><span class="token punctuation">}</span><span class="token punctuation">;</span></code></pre><p>每个玩家可用的内存容量上限为 <strong>2 MB</strong>。</p>
<p>为了方便您的使用，一些游戏对象已经被链接到了全局的 <code>Memory</code> 对象并在其中保存了自己的键。例如，你可以通过 creep 的 <code>memory</code> 属性来访问到它的内存：</p>
<pre class="highlight undefined tab-undefined "><code>Game<span class="token punctuation">.</span>creeps<span class="token punctuation">.</span>John<span class="token punctuation">.</span>memory <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token operator">...</span><span class="token punctuation">}</span><span class="token punctuation">;</span></code></pre><p>实际上，这个属性是全局对象 <code>Memory</code> 上对应键的别名：</p>
<pre class="highlight undefined tab-undefined "><code>Game<span class="token punctuation">.</span>creeps<span class="token punctuation">.</span>John<span class="token punctuation">.</span>memory<span class="token punctuation">.</span>role <span class="token operator">=</span> <span class="token string">'harvester'</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>Memory<span class="token punctuation">.</span>creeps<span class="token punctuation">.</span>John<span class="token punctuation">.</span>role<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// -> 'harvester'</span></code></pre><p>信息被保存记录在 <code>Memory</code> 对象中，但是可以通过其他游戏对象上一些对应的键来进行快捷访问。你可以由此来选择更合适的地址寻找方法。</p>
<h3 id="在-memory-中保存游戏对象" class="article-heading"><a href="#在-memory-中保存游戏对象" class="headerlink" title="在 memory 中保存游戏对象"></a>在 memory 中保存游戏对象<a class="article-anchor" href="#在-memory-中保存游戏对象" aria-hidden="true"></a></h3><p>您不应该在 memory 保存方法或者游戏对象，就像保存在”内存(<code>Memory</code>)“中那样。<code>Memory</code> 对象被设计用来保存 JSON 数据，并且无法保存活动的对象引用。被保存的对象数据会(因为引用的失效而变得)不再相关。并且，这么做还会浪费您有限的内存。</p>
<pre class="highlight undefined tab-undefined "><code><span class="token comment" spellcheck="true">// 这是个错误示例！</span>
<span class="token keyword">var</span> source <span class="token operator">=</span> creep<span class="token punctuation">.</span>pos<span class="token punctuation">.</span><span class="token function">findClosestByRange</span><span class="token punctuation">(</span>FIND_SOURCES<span class="token punctuation">)</span><span class="token punctuation">;</span>
creep<span class="token punctuation">.</span>memory<span class="token punctuation">.</span>source <span class="token operator">=</span> source<span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// ... </span>
creep<span class="token punctuation">.</span><span class="token function">moveTo</span><span class="token punctuation">(</span>creep<span class="token punctuation">.</span>memory<span class="token punctuation">.</span>source<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 返回 ERR_INVALID_TARGET (无效目标)</span></code></pre><p>相对于储存活动的对象来说，更好的方法是保存任何游戏对象都拥有的 <code>id</code> 属性，然后在需要时使用 <a href="/api/#Game.getObjectById"><code>Game.getObjectById</code></a> 来重新获取对应的游戏对象：</p>
<pre class="highlight undefined tab-undefined "><code><span class="token comment" spellcheck="true">// 这是个正确示例</span>
<span class="token keyword">var</span> source <span class="token operator">=</span> creep<span class="token punctuation">.</span>pos<span class="token punctuation">.</span><span class="token function">findClosestByRange</span><span class="token punctuation">(</span>FIND_SOURCES<span class="token punctuation">)</span><span class="token punctuation">;</span>
creep<span class="token punctuation">.</span>memory<span class="token punctuation">.</span>sourceId <span class="token operator">=</span> source<span class="token punctuation">.</span>id<span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// ...</span>
<span class="token keyword">var</span> source <span class="token operator">=</span> Game<span class="token punctuation">.</span><span class="token function">getObjectById</span><span class="token punctuation">(</span>creep<span class="token punctuation">.</span>memory<span class="token punctuation">.</span>sourceId<span class="token punctuation">)</span><span class="token punctuation">;</span>
creep<span class="token punctuation">.</span><span class="token function">moveTo</span><span class="token punctuation">(</span>source<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// OK</span></code></pre><h3 id="序列化" class="article-heading"><a href="#序列化" class="headerlink" title="序列化"></a>序列化<a class="article-anchor" href="#序列化" aria-hidden="true"></a></h3><p>Memory 对象以字符串形式保存，并在每个 tick 您脚本第一次对其进行访问时借助 <code>JSON.parse</code> 方法进行解析。解析所消耗的 CPU 会被当做您的脚本消耗。如果您愿意的话，可以使用全局变量 <a href="/api/#RawMemory"><code>RawMemory</code></a> 来编写您的序列化/反序列化器。它将把原始内存处理成字符串。实际上，默认的内存工作方式基本等同于下述代码：</p>
<pre class="highlight undefined tab-undefined "><code>Memory <span class="token operator">=</span> JSON<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>RawMemory<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//第一次访问 Memory 对象时执行</span>
<span class="token comment" spellcheck="true">// ...您的代码</span>
RawMemory<span class="token punctuation">.</span><span class="token keyword">set</span><span class="token punctuation">(</span>JSON<span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>Memory<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>您可以使用 <a href="/api/#RawMemory"><code>RawMemory</code></a> getter/setter 来实现自己的算法。</p>

              </div>
              <footer class="article-footer">
                <time class="article-footer-updated" datetime="2024-09-20T13:38:58.408Z" itemprop="dateModified">上次更新：9月 20, 2024</time>
                <a href="/scripting-basics.html" class="article-footer-prev"><i class="fa fa-chevron-left"></i><span>脚本基础</span></a><a href="/modules.html" class="article-footer-next"><span>模块</span><i class="fa fa-chevron-right"></i></a>
              </footer>
            </div>
          </div>
          <aside id="article-toc" role="navigation">
            
            <div id="article-toc-inner">
              <strong class="sidebar-title">目录</strong>
              <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#Game-对象"><span class="toc-text">Game 对象</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Memory-对象"><span class="toc-text">Memory 对象</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#在-memory-中保存游戏对象"><span class="toc-text">在 memory 中保存游戏对象</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#序列化"><span class="toc-text">序列化</span></a></li></ol></li></ol>
              <a href="#" id="article-toc-top">回到顶部</a>
            </div>
            
          </aside>
        </div>
      </article>
      <aside id="sidebar" role="navigation">
  <div class="inner"><a href="/api/" class=api-link><span>API Reference</span><img src="/img/link-external.svg"></a><a href="/index.html" class="sidebar-link">总览</a><strong class="sidebar-title">游戏环境</strong><a href="/introduction.html" class="sidebar-link">简介</a><a href="/creeps.html" class="sidebar-link">Creeps</a><a href="/control.html" class="sidebar-link">控制</a><a href="/defense.html" class="sidebar-link">防御</a><a href="/respawn.html" class="sidebar-link">重生</a><a href="/start-areas.html" class="sidebar-link">初始区域</a><a href="/resources.html" class="sidebar-link">资源</a><a href="/market.html" class="sidebar-link">市场</a><a href="/invaders.html" class="sidebar-link">NPC 入侵者</a><a href="/power.html" class="sidebar-link">超能</a><strong class="sidebar-title">脚本</strong><a href="/scripting-basics.html" class="sidebar-link">脚本基础</a><a href="/global-objects.html" class="sidebar-link current">全局对象</a><a href="/modules.html" class="sidebar-link">模块</a><a href="/debugging.html" class="sidebar-link">调试</a><a href="/game-loop.html" class="sidebar-link">游戏循环</a><a href="/commit.html" class="sidebar-link">外部提交</a><a href="/simultaneous-actions.html" class="sidebar-link">同步操作</a><a href="/cpu-limit.html" class="sidebar-link">CPU 限制</a><strong class="sidebar-title">其他</strong><a href="/architecture.html" class="sidebar-link">服务器架构</a><a href="/ptr.html" class="sidebar-link">公开测试区域 (PTR)</a><a href="/third-party.html" class="sidebar-link">第三方工具</a><a href="/auth-tokens.html" class="sidebar-link">验证令牌</a><a href="/community-servers.html" class="sidebar-link">社区服务器</a><a href="/tos.html" class="sidebar-link">服务条款</a><a href="/privacy-policy.html" class="sidebar-link">隐私政策</a><strong class="sidebar-title">资源</strong><a href="http://blog.screeps.com" class="sidebar-link">博客</a><a href="http://blog.screeps.com/categories/Changelogs/" class="sidebar-link">修改日志</a><a href="http://chat.screeps.com" class="sidebar-link">聊天室</a><a href="https://screeps.com/forum/" class="sidebar-link">论坛</a><strong class="sidebar-title">贡献文章</strong><a href="/contributed/rules.html" class="sidebar-link">贡献规则</a><a href="/contributed/advanced_grunt.html" class="sidebar-link">高级 Grunt 使用</a><a href="/contributed/modifying-prototypes.html" class="sidebar-link">修改原型</a><a href="/contributed/caching-overview.html" class="sidebar-link">缓存概述</a><a href="/contributed/ps_ubuntu.html" class="sidebar-link">私有服务器 MongoDB</a></div>
</aside>
    </div>
  </div>
</div>

    <footer id="footer" class="wrapper">
  <div class="inner">
    <div id="footer-copyright">
      &copy; 2024 <a href="https://screeps.com/" target="_blank">Screeps</a><br>
      Documentation licensed under <a href="http://creativecommons.org/licenses/by/4.0/" target="_blank">CC BY 4.0</a>.
    </div>
    <div id="footer-links">
      <a href="https://github.com/screeps-cn/docs" class="footer-link" target="_blank"><i class="fa fa-github-alt"></i></a>
    </div>
  </div>
</footer>

  </div>
  <div id="mobile-nav-dimmer"></div>
  <nav id="mobile-nav">
  <div id="mobile-nav-inner">
    
      <a href="/api/" class=api-link><span>API Reference</span><img src="/img/link-external.svg"></a><a href="/index.html" class="mobile-nav-link">总览</a><strong class="mobile-nav-title">游戏环境</strong><a href="/introduction.html" class="mobile-nav-link">简介</a><a href="/creeps.html" class="mobile-nav-link">Creeps</a><a href="/control.html" class="mobile-nav-link">控制</a><a href="/defense.html" class="mobile-nav-link">防御</a><a href="/respawn.html" class="mobile-nav-link">重生</a><a href="/start-areas.html" class="mobile-nav-link">初始区域</a><a href="/resources.html" class="mobile-nav-link">资源</a><a href="/market.html" class="mobile-nav-link">市场</a><a href="/invaders.html" class="mobile-nav-link">NPC 入侵者</a><a href="/power.html" class="mobile-nav-link">超能</a><strong class="mobile-nav-title">脚本</strong><a href="/scripting-basics.html" class="mobile-nav-link">脚本基础</a><a href="/global-objects.html" class="mobile-nav-link current">全局对象</a><a href="/modules.html" class="mobile-nav-link">模块</a><a href="/debugging.html" class="mobile-nav-link">调试</a><a href="/game-loop.html" class="mobile-nav-link">游戏循环</a><a href="/commit.html" class="mobile-nav-link">外部提交</a><a href="/simultaneous-actions.html" class="mobile-nav-link">同步操作</a><a href="/cpu-limit.html" class="mobile-nav-link">CPU 限制</a><strong class="mobile-nav-title">其他</strong><a href="/architecture.html" class="mobile-nav-link">服务器架构</a><a href="/ptr.html" class="mobile-nav-link">公开测试区域 (PTR)</a><a href="/third-party.html" class="mobile-nav-link">第三方工具</a><a href="/auth-tokens.html" class="mobile-nav-link">验证令牌</a><a href="/community-servers.html" class="mobile-nav-link">社区服务器</a><a href="/tos.html" class="mobile-nav-link">服务条款</a><a href="/privacy-policy.html" class="mobile-nav-link">隐私政策</a><strong class="mobile-nav-title">资源</strong><a href="http://blog.screeps.com" class="mobile-nav-link">博客</a><a href="http://blog.screeps.com/categories/Changelogs/" class="mobile-nav-link">修改日志</a><a href="http://chat.screeps.com" class="mobile-nav-link">聊天室</a><a href="https://screeps.com/forum/" class="mobile-nav-link">论坛</a><strong class="mobile-nav-title">贡献文章</strong><a href="/contributed/rules.html" class="mobile-nav-link">贡献规则</a><a href="/contributed/advanced_grunt.html" class="mobile-nav-link">高级 Grunt 使用</a><a href="/contributed/modifying-prototypes.html" class="mobile-nav-link">修改原型</a><a href="/contributed/caching-overview.html" class="mobile-nav-link">缓存概述</a><a href="/contributed/ps_ubuntu.html" class="mobile-nav-link">私有服务器 MongoDB</a>
    
  </div>
</nav>
  <!-- Scripts -->
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<!-- build:js build/js/main.js -->
<script src="/js/lang_select.js"></script>
<script src="/js/scrollingelement.js"></script>
<script src="/js/toc.js"></script>
<script src="/js/mobile_nav.js"></script>
<script src="/js/custom.js"></script>
<!-- endbuild -->
<script src="https://cdn.jsdelivr.net/retinajs/1.3.0/retina.min.js" async></script>

<!-- Algolia -->

</body>
</html>